package com.study.ju.util.http.jdkhttp;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;


public final class HttpsUtil{

    private final  class TrustAnyTrustManager implements X509TrustManager {

        @Override
        public void checkClientTrusted(X509Certificate[] chain, String authType)
                throws CertificateException {
        }

        @Override
        public void checkServerTrusted(X509Certificate[] chain, String authType)
                throws CertificateException {
        }

        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[] {};
        }
    }

    private final  class TrustAnyHostnameVerifier implements HostnameVerifier {
        @Override
        public boolean verify(String hostname, SSLSession session) {
            return true;
        }
    }

    private final  String userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11";
    private final  int connectTimeout = 3000;
    private final  int readTimeout = 15000;
    private final  String charset = "utf-8";
    private final  String contentType = "application/json";


    public final  String doPost(String url, String param)throws Exception{
        return doPost(url,param,contentType);
    }
    
    public final  String doPost(String url,String param,Integer connectTimeout,Integer readTimeout)throws Exception{
    	return doPost(url,param,contentType,connectTimeout,readTimeout);
    }

    public final  String doPost(String url,String param,String contentType)throws Exception{
        return doPost(url,param,contentType,connectTimeout,readTimeout);
    }

    public final  String doPost(String url,String param,String contentType,Integer connectTimeout,Integer readTimeout)throws Exception{
        return doPost(url,param,userAgent,contentType,connectTimeout,readTimeout);
    }

    public final  String doPost(String url,String param,String userAgent,String contentType,Integer connectTimeout,Integer readTimeout)throws Exception{
        return doPost(url,param,userAgent,contentType,charset,connectTimeout,readTimeout);
    }

    public final  String doPost(String url,String param,String userAgent,String contentType,String charset,Integer connectTimeout,Integer readTimeout) throws Exception {

        //判断参数是否为空
        if(null==url||"".equals(url)){
            throw new RuntimeException("url为空");
        }
        if(null==param||"".equals(param)){
            throw new RuntimeException("param为空");
        }
        if(null==userAgent||"".equals(userAgent)){
            throw new RuntimeException("userAgent为空");
        }

        if(null==contentType||"".equals(contentType)){
            throw new RuntimeException("contentType为空");
        }

        if(null==charset||"".equals(charset)){
            throw new RuntimeException("charset为空");
        }

        String result = null;
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, new TrustManager[] { new TrustAnyTrustManager() },
                new java.security.SecureRandom());
        URL console = new URL(url);
        HttpsURLConnection conn =null;
        DataOutputStream out = null;
        InputStream is = null;
        try{
            conn = (HttpsURLConnection) console.openConnection();
            conn.setSSLSocketFactory(sc.getSocketFactory());
            conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
            conn.setRequestProperty("User-Agent",userAgent);
            conn.setConnectTimeout(connectTimeout);
            conn.setReadTimeout(readTimeout);
            conn.setRequestProperty("Content-Type",contentType);
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.connect();
            out = new DataOutputStream(conn.getOutputStream());
            out.write(param.getBytes(charset));
            // 刷新、关闭
            out.flush();
            out.close();
            is = conn.getInputStream();
            if (is != null) {
                ByteArrayOutputStream outStream = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                int len = 0;
                while ((len = is.read(buffer)) != -1) {
                    outStream.write(buffer, 0, len);
                }
                is.close();
                result = new String(outStream.toByteArray(),"utf-8");
            }

        }catch (Exception e){
            throw e;
        }finally {
            try{
                if(null!=conn){
                    conn.disconnect();
                }
                if(null!=out){
                    out.close();
                }
                if(null!=is){
                    is.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }

        return result;

    }
    
    public final  String doGet(String url) throws Exception {

        //判断参数是否为空
        if(null==url||"".equals(url)){
            throw new RuntimeException("url为空");
        }
       
        String result = null;
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, new TrustManager[] { new TrustAnyTrustManager() },
                new java.security.SecureRandom());
        URL console = new URL(url);
        HttpsURLConnection conn =null;
        DataOutputStream out = null;
        InputStream is = null;
        try{
            conn = (HttpsURLConnection) console.openConnection();
            conn.setSSLSocketFactory(sc.getSocketFactory());
            conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
            conn.setRequestProperty("User-Agent",userAgent);
            conn.setConnectTimeout(connectTimeout);
            conn.setReadTimeout(readTimeout);
            conn.setRequestProperty("Content-Type",contentType);
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setRequestMethod("GET");
            conn.connect();
            out = new DataOutputStream(conn.getOutputStream());
            
            // 刷新、关闭
            out.flush();
            out.close();
            is = conn.getInputStream();
            if (is != null) {
                ByteArrayOutputStream outStream = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                int len = 0;
                while ((len = is.read(buffer)) != -1) {
                    outStream.write(buffer, 0, len);
                }
                is.close();
                result = new String(outStream.toByteArray(),"utf-8");
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try{
                if(null!=conn){
                    conn.disconnect();
                }
                if(null!=out){
                    out.close();
                }
                if(null!=is){
                    is.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }

        return result;

    }
    
    


}
